package com.huwaiwai.sort;

/**
 * 选择排序
 * Created by hww on 2018/12/7 1:23 PM.
 */
public class SelectionSort {

    public static void selectionSort(int [] a) {
        int length = a.length;
        for (int i = 0; i < length; i++) {

            int minIndex = getMinIndex(a, i);
            if (minIndex == i) {
                continue;
            }
            int temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }

    private static int getMinIndex(int [] a, int index) {
        int length = a.length;
        int min = a[index];
        int minIndex = index;
        for (int i = index + 1; i < length; i++) {
            if (a[i] < min) {
                minIndex = i;
                min = a[i];
            }
        }
        return minIndex;
    }
}
